import Vue from 'vue'
import Router from 'vue-router'
// import HelloWorld from '@/components/HelloWorld'
import Login from "@/pages/Login"//引入登录
import Menu from "@/pages/Menu"//引入菜单
import My from '@/pages/My'//引入个人设置页面

const originalPush = Router.prototype.push
Router.prototype.push = function push(location) {// 屏蔽push方法重复点击出现报错
    return originalPush.call(this, location).catch(err => err)
}
const originalReplace = Router.prototype.replace;
Router.prototype.replace = function replace(location) {// 屏蔽replace方法重复点击出现报错
    return originalReplace.call(this, location).catch(err => err);
};

Vue.use(Router)

const router= new Router({
  routes: [
    //默认配置
    // {
    //   path:'/',
    //   redirect:"/login"
    // },
    {
      path: '/',
      name: 'login',
      component: Login
    },
    {
      path:"/my",
      name:"my",
      component:My
    },
    {
      path:"/menu",
      name:"menu",
      component:Menu,
      children:[
        {
          path:"/menu",
          redirect:"selobs"
        },
        // 检测模块
        {
          name:"selobs",
          path:"selobs",
          component:()=>import("@/pages/observe/Selobs")
        },
        {
          name:"addobs",
          path:"addobs",
          component:()=>import("@/pages/observe/Addobs")
        },
        // 消毒模块
        {
          name:"selxd",
          path:"selxd",
          component:()=>import("@/pages/ster/Selxd")
        },
        {
          name:"addxd",
          path:"addxd",
          component:()=>import("@/pages/ster/Addxd")
        },
        // 任务模块
        {
          name:"myass",
          path:"myass",
          component:()=>import("@/pages/ass/Myass")
        },
        {
          name:"allass",
          path:"allass",
          component:()=>import("@/pages/ass/Allass")
        },
        {
          name:"disass",
          path:"disass",
          component:()=>import("@/pages/ass/Disass")
        },

        // 系统模块
        {
          name:"auth",
          path:"auth",
          component:()=>import("@/pages/auth/Auth")
        },
        {
          name:"man",
          path:"man",
          component:()=>import("@/pages/auth/Man")
        },
        {
          name:"role",
          path:"role",
          component:()=>import("@/pages/auth/Role")
        },

        // 数据模块
        {
          name:"obsres",
          path:"obsres",
          component:()=>import("@/pages/data/Obsres")
        },
        {
          name:"xdata",
          path:"xdata",
          component:()=>import("@/pages/data/Xdata")
        },
      ]
    }
  ]
})

//路由守卫
router.beforeEach((to,from,next)=>{
  let token=sessionStorage.getItem('token')
  if(to.path=='/'){//登录页面
    if(token){//验证通过 就不到登录页面
    next('/menu')
    }else{//验证不通过 就是在登录页面
      next()
    }
  }else{
    //非登录页面
    //验证是否登录
    if(token){
      next()
    }else{//验证失败
      next('/')
    }
  }
}
)

export default router